package 剑指offer;

/**
 * @description:
 * @author: ywk
 * @date: 2020-10-29
 */
public class 求平方根 {
    public static void main(String[] args) {
        System.out.println(sqrt(99));

    }

    /**
     * @param x int整型
     * @return int整型
     */
    public static int sqrt(int x) {
        int low = 1;
        int high = x / 2+1;
        int mid = 1;
        while (low <=high) {
            mid = (low + high) / 2;
            if (x / mid == mid) {
                return mid;
            }
            if (x / mid > mid) {
                low = mid ;
            }
            if (x / mid < mid) {
                high = mid ;
            }
        }
        return low - 1;
    }
}
